<!--
  入库明细
-->
<template>
	<a-modal title="入库明细" :width="1400" :visible="visible" :confirmLoading="confirmLoading" :footer="null"
		@ok="handleSubmit" @cancel="handleCancel">
		<a-card :bordered="false">
			<div class="table-page-search-wrapper">
				<a-form layout="inline">
					<a-row :gutter="48">

						<a-col :md="8" :sm="24">
							<a-form-item label="料号">
								<a-input v-model="queryParam.materialCode" placeholder="请输入料号" />
							</a-form-item>
						</a-col>
						<a-col :md="8" :sm="24">
							<a-form-item label="物料名称">
								<a-input v-model="queryParam.materialName" placeholder="请输入物料名称" />
							</a-form-item>
						</a-col>
						<a-col :md="8" :sm="24">
							<a-button type="primary" @click="$refs.table.refresh()">查询</a-button>
							<a-button
								@click="() => { queryParam.materialCode = null; queryParam.materialName = null; $refs.table.refresh(); }">重置</a-button>
						</a-col>
					</a-row>
				</a-form>
			</div>

			<s-table ref="table" size="default" :columns="columns" :data="loadData" :alert="true"
				:rowKey="(record) => record.code"
				:rowSelection="null && { selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" :showPagination="false">
				<template slot="completedNumber" slot-scope="text, record">
					<span v-if="record.number == record.completedNumber" style="color: #000;">{{ record.completedNumber }}</span>
					<span v-if="record.number > record.completedNumber" style="color: red;">{{ record.completedNumber }}</span>
					<span v-if="record.number < record.completedNumber" style="color: green;">{{ record.completedNumber }}</span>
				</template>
				<template slot="chazhi" slot-scope="text, record">
					<span v-if="record.completedNumber - record.number == 0" style="color: #000;">
						{{ record.completedNumber - record.number }}
					</span>
					<span v-if="record.completedNumber - record.number > 0" style="color: green;">
						+ {{ record.completedNumber - record.number }}
					</span>
					<span v-if="record.completedNumber - record.number < 0" style="color: red;">
						- {{ Math.abs(record.completedNumber - record.number) }}
					</span>
				</template>
				<!-- <span slot="image" slot-scope="text">
					<image-preview :images="text" />
				</span> -->
				<span slot="image" slot-scope="text,record">
					<image-preview1 :images="text" :imageUrl="record.material.imageUrl" />
				</span>
			</s-table>
		</a-card>
	</a-modal>
</template>

<script>
import Vue from 'vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { STable } from '@/components'
import { page, del, add } from '@/api/modular/main/emsordermaterial/emsOrderMaterialManage'
import inStoreSourceForm from '@/views/main/emsordermaterial/purchaseOrder/emsInStoreSourceForm'
import { page as emsOrderMaterialManagePage } from '@/api/modular/main/emsordermaterial/emsOrderMaterialManage'
export default {
	components: {
		STable,
		inStoreSourceForm,
	},

	data() {
		return {
			title: null,
			advanced: false, // 高级搜索 展开/关闭
			queryParam: {},  // 查询参数
			// 加载数据方法 必须为 Promise 对象
			loadData: parameter => {
				// this.queryParam.pageSize = -1
				// this.queryParam.orderId = this.$util.getUrlParam(this.$route.path)
				// this.queryParam.materialType = this.$util.getUrlParam(this.$route.path).materialType
				return emsOrderMaterialManagePage(Object.assign(parameter, this.queryParam)).then((res) => {
					return res.data
				})
			},
			selectedRowKeys: [],
			selectedRows: [],
			defaultExpandedKeys: [],
			visible: false,
			confirmLoading: false,
			emsOrder: null,
			viewStateEnum: {
				normalSupplier: 'normalSupplier',
				primarySupplier: 'primarySupplier',
				purchaseOrderEnd: 'purchaseOrderEnd', //这个应该没有，UI和normalSupplier长的一样
			},
			viewState: '',
			extra: {},
			columns: [],
		}
	},
	computed: {
		token() {
			return Vue.ls.get(ACCESS_TOKEN)
		},
	},
	methods: {
		show(record, extra) {
			this.visible = true
			this.emsOrder = record
			this.extra = extra;
			this.queryParam.pageSize = -1
			this.queryParam.orderId = record.orderId
			this.queryParam.materialType = extra.materialType
			this.queryParam.sourceOrderMaterialId = 0;
			setTimeout(() => {
				this.handleOk()
				this.$refs.table.clearSelected()
			}, 100)
			this.columns = [
				{
					title: '序号', width: '80px',
					align: 'center',
					customRender: (text, record, index) => `${index + 1}`,
				},
				{
					title: '料号',
					dataIndex: 'materialCode'
				},
				{
					title: '物料名称',
					dataIndex: 'materialName'
				},
				{
					title: '规格',
					dataIndex: 'materialModelNumber',
					scopedSlots: { customRender: 'materialModelNumber' }
				},
				{
					title: '图片',
					dataIndex: 'image',
					scopedSlots: { customRender: 'image' }
				},
				{
					title: '采购数量',
					dataIndex: 'number',
				},
				{
					title: '已入库数量',
					dataIndex: 'completedNumber',
					scopedSlots: { customRender: 'completedNumber' }
				},
				{
					title: '待入库数量',
					dataIndex: '',
					customRender: (text, record, index) => record.number - record.completedNumber < 0 ? 0 : record.number - record.completedNumber,
				},
			]
		},
		rukuDetails(record, extra) {
			this.visible = true
			this.columns = [
				{
					title: '序号', width: '80px',
					align: 'center',
					customRender: (text, record, index) => `${index + 1}`,
				},
				{
					title: '料号',
					dataIndex: 'materialCode'
				},
				{
					title: '物料名称',
					dataIndex: 'materialName'
				},
				{
					title: '规格',
					dataIndex: 'materialModelNumber',
					scopedSlots: { customRender: 'materialModelNumber' }
				},
				{
					title: '图片',
					dataIndex: 'image',
					scopedSlots: { customRender: 'image' }
				},
				{
					title: '采购数量',
					dataIndex: 'number',
				},
				{
					title: '实收数量',
					dataIndex: 'completedNumber',
					scopedSlots: { customRender: 'completedNumber' }
				},
				{
					title: '差值',
					dataIndex: '',
					scopedSlots: { customRender: 'chazhi' }
				},
			]
			this.queryParam.pageSize = -1
			this.queryParam.orderId = record.orderId
			this.queryParam.materialType = extra.materialType
			this.queryParam.sourceOrderMaterialId = 0;
			setTimeout(() => {
				this.handleOk()
				this.$refs.table.clearSelected()
			}, 100)
		},
		handleOk() {
			this.$refs.table.refresh()
		},
		onSelectChange(selectedRowKeys, selectedRows) {
			this.selectedRowKeys = selectedRowKeys
			this.selectedRows = selectedRows
		},
		handleSubmit() {
		},
		handleCancel() {
			this.visible = false
		},
		onEmsMaterialIndex1Selected(materialRow) {
			// console.log(`选择了原料:id=${materialRow.materialId}, code=${materialRow.materialCode}, name=${materialRow.materialName}`)
			// debugger
			let { materialId, materialName, materialCode, materialType, costPrice } = materialRow
			// 需要调用接口往订单中添加物料
			let _emsOrder = Object.assign({}, this.emsOrder)
			_emsOrder.orderMaterials = []
			let orderMaterial = {
				orderMaterialId: '',
				orderId: _emsOrder.orderId,
				bomId: '0',  // 单独选择原料时没有BOMID
				materialId: materialId,
				number: 1,
				completedNumber: 0,
				executeStatus: '0',
				executeProgress: '0',
				status: '0',
				createTime: '',
				createUser: '',
				updateTime: '',
				updateUser: '',
			}
			_emsOrder.orderMaterials.push(orderMaterial)
			add(orderMaterial).then(res => {
				if (res.success) {
					this.$message.success('新增成功')
					// this.handleCancel()
					// this.$emit('ok', values)
					this.$refs.table.refresh()
				} else {
					this.$message.info('新增失败：' + res.message)
				}
			})
			// 然后刷新table控件
		},
		imageURLFormat(text) {
			return `${this.$store.state.baseUrl}/sysFileInfo/preview?id=${text}&token=${this.token}`
		}
	}
}
</script>

<style lang="less">
.table-operator {
	margin-bottom: 18px;
}

button {
	margin-right: 8px;
}
</style>
